(function(){
	var api = 'http://h6.duchengjiu.top/shop/';
	
	var goodsId = getUrlValue('goods_id');
	var oJzbox = document.getElementById('jzbox');
	//console.log(oJzbox);
	//console.log(goodsId);
	$.get(api+'api_goods.php',{'goods_id':goodsId},function(data){
		
		console.log(data);
		var obj=data.data[0];
		var yuanjia = parseInt(obj.price)+600;
		//console.log(yuanjia);
		oJzbox.innerHTML = `
				<p class='lcwz'>良仓 > 餐厨 > <span class='cydq'>厨用电器 </span>> 畅销日本的蒸汽烤箱 | 5档可选 温度控制细致入微</p>
				<div class='jzxone fl'>
					<div class="photobig">
						<img src="img/jzz.gif" lazyloadsrc="${obj.goods_thumb}" alt="" />
					</div>
					<div class="photosmall"></div>
				</div>
				<div class="jzxtwo fl">
					<p>❤730</p>
					<p><span>${obj.goods_name}</span><br/>${obj.goods_desc}</p> 
					<p>价格:&nbsp;&nbsp;<span>¥${obj.price}元</span><span> ¥${yuanjia}元</span></p>
					<div class='nums clearfix'>
						<p class='fl'>数量：</p>
						<span id='reduce'>-</span>
						<input type="text" id='number' value='1'/>
						<span id="add">+</span>
					</div>
					<div class="goshoping" id='shopingpro'>立即购买</div>
					<div class="carts">
						<div class="fl" id='addcart'>加入购物车</div>
						<div class="fl">分享</div>
					</div>
				</div>
		`;
		$('[lazyloadsrc]').YdxLazyLoad();
		
		var oReduce = document.getElementById('reduce');
		var oNumber = document.getElementById('number');
		var oAdd = document.getElementById('add');
		
		var nowNum = parseInt(oNumber.value);
		oReduce.onclick = function(){
			nowNum--;
			oNumber.value = nowNum;
		};
		oAdd.onclick = function(){
			nowNum++;
			oNumber.value = nowNum;
		};
		
		
		//获取两个点击按钮
		var shopingPro = document.getElementById('shopingpro');
		var addCart = document.getElementById('addcart');
		
		//加入购物车
		addCart.onclick = function(){
			var token = localStorage.getItem('token');
			if(token){
				$.post(api+'api_cart.php?token='+token,{'goods_id':goodsId,'number':nowNum},function(data){
					if(data.code ==0 ){
						alert(data.message);
					}else if(data.code == 2){
						alert('此商品已添加购物车了，请查看购物车');
					};
					
				});
			}else{
				if(alert('您还未登录，点击确定跳转到登录页面')){location.href = 'login.html?goods_id='+goodsId;	};
					
			}
		};
		
		//点击立即购买
		shopingPro.onclick = function(){
			var token = localStorage.getItem('token');
			if(token){
				location.href = 'cart.html';
			}else{
				if(confirm('您还未登录，点击确定跳转到登录页面')){
					location.href = 'login.html?goods_id='+goodsId;
				};
				
			};
		};
	});
	
})();

//这个方法用来干 什么的？
function getUrlValue(name){//?goods_id=257557&age=haha
		var str = location.search.substr(1);//goods_id=257557
		//console.log(location);
		//console.log(location.search);
		//console.log(location.search.substr);
		//console.log(location.search.substr(1));
		var reg = new RegExp("(^|&)"+name+"=([^&]*)(&|$)");
		var result = str.match(reg);
		//console.log(reg);
		//console.log(result)
		//console.log(result);
		if(result === null) return null;
		return result[2];
};